

function filterConfig(config, opts = {}) {
  const {
    filterTopLevel = false,
    applyExcludeFilter = false,
    regex = new RegExp(
      [
        '\\\\d+\\s*倍(?:流量)?',                 // 2倍, 20倍流量
        '倍流量',
        '倍率',
        '双倍|三倍|四倍|五倍|六倍|七倍|八倍|九倍|十倍|二十倍',
        '[xX×]\\s*(?:[2-9]|1[0-9]|20)(?!\\d)', // x5/x10/×20
        '(?:[2-9]|1[0-9]|20)\\s*[xX×](?!\\d)'  // 5x/10X/20×（兼容少见写法）
      ].join('|'),
      'i'
    )
  } = opts;

  const cfg = structuredClone ? structuredClone(config) : JSON.parse(JSON.stringify(config));

  const isBanned = (name) => typeof name === 'string' && regex.test(name);

  // 过滤顶层 proxies（可选）
  if (filterTopLevel && Array.isArray(cfg.proxies)) {
    cfg.proxies = cfg.proxies.filter(p => !(p && p.name && isBanned(p.name)));
  }

  // 过滤每个分组的静态 proxies
  if (Array.isArray(cfg['proxy-groups'])) {
    cfg['proxy-groups'] = cfg['proxy-groups'].map(g => {
      const ng = { ...g };
      if (Array.isArray(ng.proxies)) {
        ng.proxies = ng.proxies.filter(item => !isBanned(String(item)));
      }
      // 对 provider/use 分组，写入 exclude-filter（可选）
      if (applyExcludeFilter && (ng.use || ng['proxy-providers'])) {
        ng['exclude-filter'] = regex.source;
        // 避免与 filter 冲突
        delete ng.filter;
      }
      return ng;
    });
  }

  return cfg;
}

